<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\Cabinet;
use Dcat\Admin\Admin;
    /**
     * @param $url 请求网址
     * @param bool $params 请求参数
     * @param int $ispost 请求方式
     * @param int $https https协议
     * @return bool|mixed
     */
    function Request_curl($url, $params = false, $ispost = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }

    function getAdminId($adminId){
        $adminuser = Db::table('admin_users')->where(['id'=>$adminId])->first();
        $role = Db::table('admin_role_users')->where(['user_id'=>$adminId])->first();
        switch ($role->role_id) {
            case 1:
                return 1;
                break;
            // case 2:
            //     return $adminuser->id;
            //     break;
            case 3:
                return $adminuser->pid;
                break;
            case 4:
                return $adminuser->id;
                break;
            case 5:
                return $adminuser->pid;
                break;
        }
    }

    //鉴权
    function authentication(){
        
        $data = 'appId=86y97uYn78Try5i1zFdTLiErnX4a&secret=aayy3kB2YJGh1FCpeyxGsyC_uCAa';
        $url = 'https://device.api.ct10649.com:8743/iocm/app/sec/v1.1.0/login';
        $header = ["Content-Type" => "application/x-www-form-urlencoded;"];//貌似不需要,默认就是
        $a = postSsl($url,$data,$header);
        if($a['status']==1){
            $return = json_decode($a['return'],1);
            Cache::add('authtoken', $return['accessToken'], 3000);
        }
    }
    function postSsl($url, $data, $header) {
        //if (is_array($data)) {
        //    $data = http_build_query($data);
        //}
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);// 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_SSLCERT,dirname(dirname(__FILE__)).'/server.crt');
        curl_setopt($curl, CURLOPT_SSLKEY, dirname(dirname(__FILE__)).'/server.key');
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $res = curl_exec($curl); // 执行操作
        $ret = ['status' => 1];
        if ($error = curl_errno($curl)) {
            $ret['status'] = 0;
            $ret['return'] = $error;
        } else {
            $ret['return'] = $res;
        }
        curl_close($curl); // 关闭CURL会话
        return $ret; // 返回数据
    }

    function headmenu($id,$selected=1){
        Admin::script(
            <<<JS
    $(".content-header").remove();
JS            
        );
        $cabinet = Cabinet::find($id);
        $projectId = $cabinet['project_id'];
        //盒子
        if (empty($cabinet['pid'])) {
            $goToId = $id;
        //柜子
        } else {
            $goToId = $cabinet['pid'];
        }
        $realtimeUrl = url('admin/cabinet-realtime').'/'.$projectId.'/'.$goToId;
        $historyUrl = url('admin/cabinet-history').'/'.$goToId;
        $compareUrl = url('admin/cabinet-compare').'/'.$goToId;
        $customLineUrl = url('admin/cabinet-custom').'/'.$projectId.'/'.$goToId;
        $alarmLogUrl = url('admin/cabinet-alarm-logs').'/'.$goToId;
        $alarmUrl = url('admin/cabinet-alarms').'/'.$goToId;
        $liveTextUrl = url('admin/cabinet-live-texts').'/'.$goToId;
        $downloadUrl = url('admin/cabinet-second-backup').'/'.$goToId;
        $historyTempUrl = url('admin/cabinet-history-temp').'/'.$goToId;
        $historyLineTempUrl = url('admin/cabinet-history-line-temp').'/'.$goToId;
        $uploadUrl = url('admin/upload-to-chart').'/'.$goToId;

        $realtime['color'] = $selected==1?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $realtime['png'] = $selected==1?'icon/realtime1.png':'icon/realtime.png';
        $realtime = $realtime?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/realtime.png');
        $history['color'] = $selected==2?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $history['png'] = $selected==2?'icon/history1.png':'icon/history.png';
        $history = $history?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/history.png');
        $customLine['color'] = $selected==3?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $customLine['png'] = $selected==3?'icon/custom1.png':'icon/custom.png';
        $customLine = $customLine?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/custom.png');
        $alarm['color'] = $selected==4?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $alarm['png'] = $selected==4?'icon/alarm1.png':'icon/alarm.png';
        $alarm = $alarm?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/alarm.png');
        $alarmLog['color'] = $selected==5?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $alarmLog['png'] = $selected==5?'icon/alarmlog1.png':'icon/alarmlog.png';
        $alarmLog = $alarmLog?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/alarmlog.png');
        $liveText['color'] = $selected==6?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $liveText['png'] = $selected==6?'icon/livetext1.png':'icon/livetext.png';
        $liveText = $liveText?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/livetext.png');
        $download['color'] = $selected==7?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $download['png'] = $selected==7?'icon/download1.png':'icon/download.png';
        $download = $download?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/download.png');
        $upload['color'] = $selected==8?'background-color:#3DA1E3;color:#fff;width:19.5%;height:72px;margin: 1px;':'background-color:#fff;width:19.5%;height:72px;margin: 1px;';
        $upload['png'] = $selected==8?'icon/upload1.png':'icon/upload.png';
        $upload = $upload?:array('color'=>'background-color:#fff;width:19.5%;height:72px;margin: 1px;','png'=>'icon/upload.png');
        // print_r($history);die;
        // onclick="window.open(\''.$realtimeUrl.'\',\'_self\')"background-color:#3DA1E3;color:#fff;
        return '<div style=\'font-size:14px;height:72px;display: flex;flex-direction: row;width: 100%;text-align:center;margin-top:-10px;margin-bottom:20px;\'>
                    <div style='.$realtime['color'].' onclick="window.open(\''.$realtimeUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($realtime['png']).'><div style=\'height:15px;\'>实时数据</div>
                    </div>
                    <div style='.$history['color'].' onclick="window.open(\''.$historyUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($history['png']).'><div style=\'height:15px;\'>历史记录</div>
                    </div>
                    <div style='.$customLine['color'].' onclick="window.open(\''.$customLineUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($customLine['png']).'><div style=\'height:15px;\'>特性曲线</div>
                    </div>
                    <div style='.$alarm['color'].' onclick="window.open(\''.$alarmUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($alarm['png']).'><div style=\'height:15px;\'>报警设置</div>
                    </div>
                    <div style='.$alarmLog['color'].' onclick="window.open(\''.$alarmLogUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($alarmLog['png']).'><div style=\'height:15px;\'>报警记录</div>
                    </div>
                    <div style='.$liveText['color'].' onclick="window.open(\''.$liveTextUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($liveText['png']).'><div style=\'height:15px;\'>实况文本</div>
                    </div>
                    <div style='.$download['color'].' onclick="window.open(\''.$downloadUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($download['png']).'><div style=\'height:15px;\'>数据备份</div>
                    </div>
                    <div style='.$upload['color'].' onclick="window.open(\''.$uploadUrl.'\',\'_self\')">
                        <img style=\'margin-top:14px;margin-bottom:2px;\' src='.url($upload['png']).'><div style=\'height:15px;\'>数据上传</div>
                    </div>
                </div>';
    }
?>